package durose.modular.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import durose.system.dict.CommonDict;

import durose.system.pojo.Dept; 
import durose.system.service.DeptService;
 import durose.system.springUtil.SpringDeptFactory; 

 import com.github.pagehelper.Page;
import durose.system.dict.ResponseDict;

@Controller
public class DeptController {
	String prefix = "system/modular/dept/";
	
	
	
	@Autowired
	DeptService deptService;
	
	
	@RequestMapping("dept")
	public String deptGet(){
		return prefix+"dept";
	}
	
	@RequestMapping("dept_list")
	@ResponseBody
	public Object deptList(@RequestParam(value="pageNumber",defaultValue="0")Integer pagenumber,@RequestParam(value="queryName",required=false) String queryName){
		PageHelper.startPage(pagenumber,CommonDict.PAGE_SIZE);
		List<Dept> deptes = deptService.deptList(queryName);
		//把从数据库中查询的pojo数据与所关联的表进行处理(如果有关联的话)
		List<Map<String,Object>> mapList = SpringDeptFactory.me().initData((Page)deptes);
		PageInfo<Map<String,Object>> page = new PageInfo<>(mapList);
		return page;
	}
	
	@RequestMapping("dept_add")
	@ResponseBody
	public Object deptAdd(Dept pojo){
		deptService.deptAdd(pojo);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	@RequestMapping("dept_add_page")
	public String deptAddPage(Model model){
		
		return prefix+"dept_add";
	}
	
	@RequestMapping("dept_edit")
	@ResponseBody
	public Object deptEdit(Dept pojo){
		deptService.deptUpdate(pojo);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	@RequestMapping("dept_edit_page")
	public String  deptEditPage(int id,Model model){
		
		model.addAttribute("dept", deptService.deptGet(id));
		return prefix+"dept_edit";
	}
	
	@RequestMapping("dept_delete")
	@ResponseBody
	public Object deptDelete(int id){
		deptService.deptDelete(id);
		return new ResponseDict(CommonDict.BUS_STATUS_OK);
	}
	
	
}
